//
//  GMChoseVC.m
//  Teecloud
//
//  Created by 小熊 on 2018/12/27.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "GMChoseVC.h"
#import "GMOrgCell.h"
#import "GMOrgModel.h"
#import "CYLTabBarController.h"
@interface GMChoseVC ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *searchTextTF;
@property (weak, nonatomic) IBOutlet UITableView *tabView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, copy) NSString *searchKey;

@end

@implementation GMChoseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择运营机构";
    self.searchKey = @"";
    self.tabView.dataSource = self;
    self.tabView.delegate = self;
    self.searchTextTF.delegate = self;
    [self.tabView registerNib:[UINib nibWithNibName:@"GMOrgCell" bundle:nil] forCellReuseIdentifier:@"GMOrgCell"];
    [self getData];
}

-(void)getData{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    [dict setValue:self.searchKey forKey:@"key"];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HJNetwork HTTPWithMethod:HJRequestMethodPOST url:[NSString stringWithFormat:@"do-api.ashx?$m=GetCompanyList&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyNetworkElseCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                    //如果msg为空，就弹出运营列表
                NSArray *arr = [GMOrgModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:RESULT_NAME_Data]];
                [self.dataArr removeAllObjects];
                [self.dataArr addObjectsFromArray:arr];
                [self.tabView reloadData];
            }
        }else{
            [MBProgressHUD showErrorOrSuc:@"请求出错" toView:self.view];
        }
    }];
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GMOrgCell *cell =[tableView dequeueReusableCellWithIdentifier:@"GMOrgCell" forIndexPath:indexPath];
    GMOrgModel *model = self.dataArr[indexPath.row];
    cell.Name.text = model.Name;
    cell.OrgName.text = [NSString stringWithFormat:@"(%@)",model.OrgName];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GMOrgModel *model = self.dataArr[indexPath.row];
    /**存储选中运营机构信息*/
    [XGMArchiverTools cache:model.BusiOrgId ForKey:BUSIORGID];
    [XGMArchiverTools cache:model.Name ForKey:BUSName];
    [XGMArchiverTools cache:model.OrgName ForKey:BUSOrgName];
    [XGMArchiverTools removeForKey:USER_UserId];
    [XGMArchiverTools removeForKey:USERPASSWD];
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.25 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self cyl_popSelectTabBarChildViewControllerAtIndex:0 completion:^(__kindof UIViewController *selectedTabBarChildViewController) {
            
        }];
    });
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    self.searchKey = textField.text;
    [self getData];
}
@end
